package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐和相关菜品信息
     * @param setmealDTO
     */
    @Override
    @Transactional
    @AutoFill(OperationType.INSERT)
    public void saveWithDish(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO ,setmeal);
        //插入套餐信息
        setmealMapper.insert(setmeal);
        //通过sql语句返回，获取id
        Long id = setmeal.getId();
        //判断套餐相关菜品是否为空
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            //插入id
           setmealDishes.forEach( setmealDish -> {
               setmealDish.setSetmealId(id);
            });
        }
        //插入套餐相关菜品信息
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 套餐的起售和禁售
     * @param status
     * @param id
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    public void setmealStartAndStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，
        //有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE){
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            dishes.forEach(dish -> {
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                                .id(id)
                                .status(status)
                                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 套餐的分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //根据id查询套餐
       Setmeal setmeal = setmealMapper.getById(id);
       //根据套餐id查询相关菜品
       List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);

       SetmealVO setmealVO = new SetmealVO();
       BeanUtils.copyProperties(setmeal ,setmealVO);
       setmealVO.setSetmealDishes(list);
       return setmealVO;
    }

    /**
     * 更新套餐信息
     * @param setmealDTO
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO , setmeal);
        //更新套餐信息
        setmealMapper.update(setmeal);
        //获取id
        Long id = setmealDTO.getId();
        //关联菜品执行先删除在增加进行修改
        //1.先删除与setmealid相关联的菜品
        setmealDishMapper.delete(id);
        //2.重新插入修改的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            //插入id
            setmealDishes.forEach( setmealDish -> {
                setmealDish.setSetmealId(id);
            });
        }
        //插入套餐相关菜品信息
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 批量删除套餐信息
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否在售卖状态
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //1.根据id删除套餐信息
        /*ids.forEach(id ->{
            setmealMapper.delete(id);
        });*/
        setmealMapper.delete(ids);

        //2.根据套餐id删除相对应的菜品
        setmealDishMapper.deleteBatch(ids);
    }
}
